<template>
	<view class="login">
		<u-toast ref="uToast"></u-toast>
		<!--  #ifdef MP-WEIXIN -->
		<u-button size="default" type="success" class="wxloginBtn" @click="wxLogin">授权登录</u-button>
		<!--  #endif -->
		<view class="hint">
			<u-radio-group v-model="radio" :size='30'>
				<u-radio name='check' active-color="green" :label-size='30'><text>我已阅读并同意'用户协议'和'隐私政策'</text></u-radio>
			</u-radio-group>
		</view>
		<u-popup v-model="modalShow" mode='center' width="500rpx" height="200rpx" border-radius='30'
			style="display: flex;">
			<u-button size="medium" type="success" class="phoneBtn" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">获取手机号</u-button>
		</u-popup>
	</view>
</template>
​
<script>
	import request from '@/util/request.js';
	export default {
		data() {
			return {
				logining: false,
				radio: '',
				modalShow: false,
				userInfo: {},
				userPhone: '',
			};
		},
		onLoad() {},
		methods: {
			wxLogin() {
				const that = this;
				if (that.radio) {
					wx.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							console.log(res)
							// console.log('打开弹窗')
							that.modalShow = true
							// uni.setStorageSync('userInfo', res.userInfo)
							that.userInfo = res.userInfo

						}
					})
				} else {
					that.$refs.uToast.show({
						title: '请勾选《用户协议和隐私政策》'
					})
				}
			},
			getPhoneNumber(value) {
				const that = this				
				console.log(value)
				
				//#ifdef MP-WEIXIN
				if (value.detail.errMsg != 'getPhoneNumber:ok') {
					that.$refs.uToast.show({
						title: value.detail.errMsg
					})
				} else {
					request.httpPost('mini/getPhoneNumber', {
						'code': value.detail.code
					}, false).then(res => {
						console.log(res)
						if (res.httpCode == 200) {
							// uni.setStorageSync('userPhone',res.data)
							that.userPhone = res.data
							var data = {
								user: JSON.stringify({
									marketShopCode: that.$store.state.user.scene,
									nickName: that.userInfo.nickName,
									avatarUrl: that.userInfo.avatarUrl,
									phoneNumber: that.userPhone,
									wxOpenId: uni.getStorageSync('wxOpenId')
								})
							}
							request.httpPost('mini/miniRegister', data, false).then(res => {
								console.log(res)
								if (res.httpCode == 200) {
									uni.setStorageSync('userId', res.data.id)
									that.$store.commit('user/SET_USERINFO', res.data)
									uni.switchTab({
										url: '/pages/ucenter/ucenter'
									});
								}
							})
						}
					})
				}
				//#endif
			}
		}
	};
</script>
​
<style scoped lang="scss">
	.login {
		display: flex;
		overflow: hidden;
		background: #fff;
		flex-direction: column;
		justify-content: center;

	}

	.wxloginBtn {
		display: inline-block;
		margin: 0 auto;
		margin-top: 50%;
		width: 500rpx;
	}

	.hint {
		margin: 20rpx auto;
		padding: 20rpx;
	}

	.phoneBtn {
		display: inline-block;
		margin: 60rpx auto;
		margin-left: 23%;
	}
</style>
